
class Solution {
    public static int findTargetSumWays(int[] nums, int target) {
        int n = nums.length;
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }

        // 如果目标值超过了所有数的总和，无法达到
        if (target > sum || (sum + target) % 2 != 0) {
            return 0;
        }
        if ( target < 0 && sum < -target) return 0;
        int capacity = (sum + target) / 2;
        int[] dp = new int[capacity + 1];
        dp[0] = 1;

        for (int i = 0; i < n; i++) {
            for (int j = capacity; j >= nums[i]; j--) {
                dp[j] += dp[j - nums[i]];
            }
        }

        return dp[capacity];
    }
}
//leetcode submit region end(Prohibit modification and deletion)
